<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
    :close-on-click-modal="false"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-row>
        <el-col :span="12">
          <el-form-item label="角色" prop="systemRole.id">
            <el-select v-model="form.systemRole.id" filterable placeholder="请选择">
              <el-option
                v-for="item in userRoles"
                :key="item.id"
                :label="item.label"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="上级部门" >
            <treeselect
              v-model="form.deptId"
              :options="superdept"
              :normalizer="normalizer"
              :show-count="true"
              placeholder="选择上级部门"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="用户名" prop="username">
            <el-input v-model.trim="form.username" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="密码" prop="password">
            <el-input v-model.trim="form.password" type="password" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系电话" prop="phone">
            <el-input v-model.trim="form.phone" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系人" prop="contact">
            <el-input v-model.trim="form.contact" />
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>


  </el-dialog>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { doAdd, doEdit, getUserRoles,getUserById } from "@/api/userManagement";
import { getdeptTree } from "@/api/departmentManagement";
import stockInandOut from "@/views/roomstore/storeData/stockInandOut";

export default {
  name: "UserManagementEdit",
  components: { Treeselect },
  data() {
    return {
      superdept: [],
      form: {
        //TODO 这个占时先写死 后面才做获取实现
        wsId: 1,
        deptId:undefined,
        systemRole: {
          id: undefined
        }
      },
      rules: {
        username: [
          { required: true, trigger: "blur", message: "请输入用户名" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入密码" }
        ],
        phone: [{ required: true, trigger: "blur", message: "请输入联系电话" }],
        contact: [{ required: true, trigger: "blur", message: "请选择联系人姓名" }]
      },
      title: "",
      dialogFormVisible: false,
      userRoles: []
    };
  },
  methods: {
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.label,
        children: node.children
      };
    },
    showEdit(row) {
      if (!row) {
        this.title = "添加";
        getUserRoles().then(Response => {
          this.userRoles = Response.data;
        });
        this.superdept = [];
        getdeptTree().then(Response => {
          this.superdept = Response.data;
        });
      } else {
        this.title = "编辑";
        getUserById(row.id).then(Response =>{
          this.form = Response.data
        })
        getUserRoles().then(Response => {
          this.userRoles = Response.data;
        });
        this.superdept = [];
        getdeptTree().then(Response => {
          this.superdept = Response.data;
        });
      }
      this.dialogFormVisible = true;
    },
    close() {
      this.$refs["form"].resetFields();
      this.form = this.$options.data().form;
      this.dialogFormVisible = false;
    },
    save() {
      this.$refs["form"].validate(async (valid) => {
        if (valid && this.form.id) { // 编辑
          const { message ,status} = await doEdit(this.form);
          if(status === 200){
            this.$baseMessage(message, "success", "vab-hey-message-success");
            this.$emit("fetch-data");
            this.close();
          }else{
            this.$baseMessage(message, 'error', 'vab-hey-message-error')
          }
        } else if (valid && !this.form.id) { // 添加
          const { message ,status} = await doAdd(this.form);
          if(status === 200){
            this.$baseMessage(message, "success", "vab-hey-message-success");
            this.$emit("fetch-data");
            this.close();
          }else{
            this.$baseMessage(message, 'error', 'vab-hey-message-error')
          }
        }
      });
    }
  }
};
</script>
